package control
{
	import events.MyEvent;
	
	import utils.MyTimer;

	public class TimerHolder
	{
		private static var myTimer:MyTimer;
		private static var haveComplete:Boolean=false;
		private static var passedTime:uint=0;
		private static var repeatCount:uint=0;
		public static function completeSetNull():void
		{
			haveComplete=false;
		}
		
		public static function get onTimeComplete():Function
		{
			return _onTimeComplete;
		}

		public static function set onTimeComplete(value:Function):void
		{
			haveComplete=true;
			_onTimeComplete = value;
		}

		private static var _onTimeComplete:Function;
		public function TimerHolder()
		{
		}
		
		public static function pause():void
		{
			myTimer.stop();
		}
		
		public static function resume():void
		{
			myTimer.start();
		}

		public static function get _myTimer():MyTimer
		{
			if(!myTimer)
			{
				trace("NO TIME");
				myTimer=new MyTimer(null,1);
			}
			return myTimer;
		}
		
		public static function startTimeWork(delay:Number,fun:Function,_repeat:uint=0):void
		{
			if(myTimer)
			{
				myTimer.GC();
			}
			repeatCount=_repeat;
			myTimer=new MyTimer(fun,delay,_repeat);
			myTimer.addEventListener(myTimer.TIME_COMPLETE,handleCountOver);
			myTimer.start();
		}
											 
	 	private static function handleCountOver(e:MyEvent):void
		{
			if(haveComplete)
			{
				try
				{
					onTimeComplete();
				}
				catch(ev:Error)
				{
					trace("onTimeComplete is not set up");
				}
				
			}
			
		}
		
		
											 
		public static function removeTimer():void
		{
			if(myTimer)
			{
				myTimer.removeEventListener(myTimer.TIME_COMPLETE,handleCountOver);
				myTimer.stop();
				myTimer.GC();
				
			}
			haveComplete=false;
		}
		
											 
											 
											 
	}
}